<?php 
	namespace Home\Controller;
	
	class HonorController extends HomeController{
		private $_model = null; //数据库操作类

		//初始化操作
		public function _initialize(){
			parent::_initialize();
			$this->_model = M('Honor');
		}

		//列表详情
		public function index(){
	       $data = M('Honor')->select();
	        // dump($data);exit;
	       $this->assign('title','文化墙');
	       $this->assign('list',$data);
	       $this->display();
	    }

		
		//加载头像修改模板
		public function tx(){
		//查出数据
		$vo = $this->_model->where(array('id'=>array('eq',I('id'))))->find();
		//向模板分配数据
		$this->assign('vo',$vo);
		//加载模板
		$this->display();
		}
			
			

			

		// 文件上传
	    public function upload1(){ 
			$upload = new \Think\Upload();
			$where='id='.$_POST['id'];
			$time=date("Ymdhis",time());
	
			 // dump($address);exit;
			 // dump($where);exit;
			// 实例化上传类
			$upload -> maxSize = 3145728;
			// 设置附件上传大小
			$upload -> exts = array('jpg', 'gif', 'png', 'jpeg');
			// 设置附件上传类型
			$upload -> rootPath = './Public/YHX_PUBLIC/Uploads/';
			// 设置附件上传根目录
			$upload -> savePath = '';
			$upload->autoSub=false;
			// 设置附件上传（子）目录
	        $upload->thumb = true;//生成缩略图
	        $upload->thumbPath = '';//生成缩略图
	       	$upload->saveName = $time.rand(1,9);//文件重命名
        	$upload->thumbMaxWidth = '50,200';//缩略图最大宽度
        	$upload->thumbMaxHeight = '50,200';//缩略图最大高度
       		$upload->thumbRemoveOrigin = true;
    
				      $info = $upload -> upload();
				      // dump($info);exit;
				      
				if (!$info) {// 上传错误提示错误信息
				$this -> error($upload -> getError());
				} else {// 上传成功
				foreach($info as $file){
					 $tuxiang = $this->_model->where($where)->getfield('himage');
				  // dump($tuxiang);exit;
				$file_path="./Public/YHX_PUBLIC/Uploads/".$file['savePath'].$file['savename'];
				$file_mini="./Public/YHX_PUBLIC/Limage/".$file['savePath'].$file['savename'];
				$data=$file['savename'];
					 // dump($data);exit;
					unlink("./Public/YHX_PUBLIC/Uploads/".$file['savePath'].$tuxiang);
					unlink("./Public/YHX_PUBLIC/Limage/".$file['savePath'].$tuxiang);
					
					if($this->_model->where($where)->setField('himage',$data) >= 0){
						$this->success("修改成功！",U('Honor/index'));
					}else{
						$this->error("修改失败");
					}

				$image = new \Think\Image(); 
				$image->open($file_path);
				// 按照原图的比例生成一个最大为100*100的缩略图并保存为thumb.jpg
				$image->thumb(100, 100)->save($file_mini);

				}

				return $info;
				}
		}

		
		
	}	
